package com.infinitus.nginxInfo.service.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

/**
 * 中文处理
 */
public class ChineseUtill {

    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
            return true;
        }
        return false;  
    }
    
    public static boolean isChinese(String nick) throws UnsupportedEncodingException{
        nick   =   new String(nick.getBytes( "iso8859-1"),   "GBK"); 
        char   ws[]   =   new   char[]{'"', '?', ' ', '\'', '&'}; 
        for(int i = 0;   i < nick.length(); i++) 
        { 
            char   c   =   nick.charAt(i); 
            //System.out.println((int)c); 
            for(int j = 0; j < ws.length; j++) 
            { 
                char v = ws[j]; 
                //System.out.println( "要验证的字符是:   | "   +   v   +   "|   !=   | "   +   c   +   "| "); 
                if (c == v) 
                { 
                    //out.println( " <script> alert( '对不起,   您的昵称中包含不能正常显示的字符,   请去掉特殊字符后重试 ');history.back(); </script> "); 
                    return false; 
                } 
            } 
            if((int)c == 0xfffd) 
            { 
                //out.println( " <script> alert( '对不起,   您的昵称中包含不能正常显示的字符,   请去掉特殊字符后重试 ');history.back(); </script> "); 
                return false; 
            } 
        }
        return true;

    }
    
    /**
     * 是否有乱码
     * @param strName
     * @return
     */
    public static boolean isMessyCode(String strName) {  
        Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
        Matcher m = p.matcher(strName);  
        String after = m.replaceAll("");  
        String temp = after.replaceAll("\\p{P}", "");  
        char[] ch = temp.trim().toCharArray();  
        float chLength = 0 ;  
        float count = 0;  
        for (int i = 0; i < ch.length; i++) {  
            char c = ch[i];  
            if (!Character.isLetterOrDigit(c)) {  
                if (!isChinese(c)) {  
                    count = count + 1;  
                }  
                chLength++;
            }  
        }  
        float result = count / chLength ;  
        if (result > 0.4) {  
            return true;  
        } else {  
            return false;  
        }  
    }  
    

    private  static  String  toGb2312(String  str)  {  
               if  (str  ==  null)  return  null;  
               String  retStr  =  str;  
               byte  b[];  
               try  {  
                       b  =  str.getBytes("ISO8859_1"); 
                       for  (int  i  =  0;  i  <  b.length;  i++)  {  
                               byte  b1  =  b[i];  
                               if  (b1  ==  63)  
                                       break;    //1  
                               else  if  (b1  >  0)  
                                       continue;//2  
                               else  if  (b1  <  0)  {        //不可能为0，0为字符串结束符 
//小于0乱码
                                       retStr  =  new  String(b,  "GB2312");  
                                       break;  
                               }  
                       }  
               }  catch  (UnsupportedEncodingException  e)  {  
                       //  e.printStackTrace();   
               }  
               return  retStr;  
       }

    /*public static void  main(String[] a) throws Exception{
    	System.out.println(toGb2312("Eå¸ç½App"));
    }*/
   
}  
